package com.fmdrama.fetcher;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.PostMethod;

import com.fmdrama.bean.Profile;

/**
 * 首页抓取器
 */
public class HomeFetcher {
	private HttpClient client = new HttpClient();
	
	/**
	 * 抓取首页
	 * @return
	 */
	public List<Profile> fetch(){
		PostMethod getMethod = null;
		try{
			getMethod = new PostMethod("http://www.koreayh.com/");
			client.executeMethod(getMethod);
			String result = getMethod.getResponseBodyAsString();
			return parse(result);
		}catch (Exception e) {
			e.printStackTrace();
		}finally{
			getMethod.releaseConnection();
		}
		return null;
	}
	
	/**
	 * 解析，主要理由正则表达式，获取我们想要的数据
	 * @param source
	 * @return
	 */
	private List<Profile> parse(String source){
		long currTime = System.currentTimeMillis();
		List<Profile> list = new ArrayList<Profile>();
		Pattern pattern = Pattern.compile("<a title=\"(.*?)\" href=\"(.*?)\">(.*?)</a>");
		Pattern blockPattern = Pattern.compile("<div class=\"index_movie_list_content\">([\\s\\S]*?)</div>");
		Matcher blockMatcher = blockPattern.matcher(source);
		int count = 0;
		int category = 0;
		while(blockMatcher.find()){
			category++;
			String sub = blockMatcher.group(0);
			Matcher matcher = pattern.matcher(sub);
			while(matcher.find()){
				try{
					count++;
					String href = matcher.group(2);
					String id = href.substring(6, href.lastIndexOf("/"));
					Profile profile = new Profile();
					profile.setId(Long.parseLong(id));
					profile.setTitle(matcher.group(1));
					profile.setCategory(category + "");
					profile.setFtime(new Date(currTime - count*1000));
					list.add(profile);
				}catch (Exception e) {
					e.printStackTrace();
				}
			}
		}
		return list;
	}
	
	public void close(){
		client.getHttpConnectionManager().closeIdleConnections(0);
	}
	
}
